package com.jeremyfeinstein.conference;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.intel.webrtc.base.Stream;
import com.jeremyfeinstein.OnCustomTouchListener;
import com.jeremyfeinstein.slidingmenu.lib.R;

/**
 * Created by sunxunchao on 2019/1/23.
 */
public class ZoomVideoDialog  {

    private static final String TAG = "ZoomVideoDialog";
    private Dialog dialog;
    private View view;
    private CustomSurfaceRenderer zoomSurfaceRenderer;
    private Stream.VideoRendererInterface zoomStreamRenderer;

    private OnZoomVideoListener onZoomVideoListener;

    @SuppressLint("ClickableViewAccessibility")
    public ZoomVideoDialog(Context context, final OnZoomVideoListener onZoomVideoListener) {
        dialog = new Dialog(context, R.style.comment_full_screen_dialog);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_zoom_view, null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        if (window != null){
            window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
            window.setWindowAnimations(R.style.popup_animation);  //添加动画
        }
        GLSurfaceView zoomSurfaceView = (GLSurfaceView) view.findViewById(R.id.zoomView);

        zoomSurfaceRenderer = new CustomSurfaceRenderer(zoomSurfaceView);
        zoomStreamRenderer = zoomSurfaceRenderer.createVideoRenderer();

        if (onZoomVideoListener!=null){
            onZoomVideoListener.onSubscribeListener(zoomStreamRenderer);
        }
        zoomSurfaceView.setOnTouchListener(new OnCustomTouchListener() {
            @Override
            protected void onDoubleClicked(View view) {
                Log.d(TAG, "onDoubleClicked: ");
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }

            @Override
            protected void onClicked(View view) {
                Log.d(TAG, "onClicked: ");
            }

            @Override
            protected void onSlidUp(View view) {
            }

            @Override
            protected void onSlidDown(View view) {
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (onZoomVideoListener!=null){
                    onZoomVideoListener.onUnSubscribeListener();
                }
                cleanZoomStream(zoomSurfaceRenderer, zoomStreamRenderer);
            }
        });
    }


    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public boolean isShowing() {
        if (dialog != null) {
            return dialog.isShowing();
        }else {
            return false;
        }
    }

    /**
     * 释放大视频资源
     *
     * @param zoomSurfaceRenderer
     * @param zoomStreamRenderer
     */
    private void cleanZoomStream(CustomSurfaceRenderer zoomSurfaceRenderer,
                                 Stream.VideoRendererInterface zoomStreamRenderer) {
        if (zoomStreamRenderer != null) {
            zoomStreamRenderer.cleanFrame();
        }
        if (zoomSurfaceRenderer != null) {
            zoomSurfaceRenderer.cancel();
            zoomSurfaceRenderer.dispose();
        }
        zoomStreamRenderer = null;
        zoomSurfaceRenderer = null;
    }


    public interface OnZoomVideoListener{
        void onSubscribeListener(Stream.VideoRendererInterface zoomStreamRenderer);

        void onUnSubscribeListener();
    }
}
